//
//  LoginView.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/20.
//  Copyright © 2020 tym. All rights reserved.
//

#import "LoginView.h"
#import "Sizes.h"
#import "Colors.h"
#import "UIimage+Extends.h"
#import "WHToast.h"
#import "UIButton+Gradient.h"
#import "UIView+ZFFrame.h"
@implementation LoginView

-(void)awakeFromNib{
    [super awakeFromNib];
     
    self.goBtn.layer.cornerRadius = 5;
    self.goBtn.layer.masksToBounds = YES;
    self.goBtn.zf_width = ScreenWidth - 60;
    [self.passFid setSecureTextEntry:YES];
    [self changedTextField:nil];
    [self.phoneFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.passFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"phoneUserDefaults"]) {
        self.phoneFid.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneUserDefaults"];
    }
}

-(void)changedTextField:(UITextField*)fid{
    if (self.phoneFid.text.length < 11 || self.passFid.text.length<1) {
        [self.goBtn setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.goBtn.bounds.size] forState:UIControlStateNormal];
    }else{
        [self.goBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }
}

-(IBAction)doChangePasswordIamge:(UIButton*)sender{
    
    if (sender.isSelected) {
        [sender setImage:[UIImage imageNamed:@"icon_login9"] forState:UIControlStateNormal];
        [self.passFid setSecureTextEntry:NO];
    }else{
        [sender setImage:[UIImage imageNamed:@"icon_login6"] forState:UIControlStateNormal];
        [self.passFid setSecureTextEntry:YES];
    }
    sender.selected = !sender.selected;
}

-(IBAction)affirmAction:(UIButton*)sender{
    
    if (self.phoneFid.text.length < 11) {
        [WHToast showMessage:@"请检查您输入的手机号码" duration:2 finishHandler:nil];
        return;
    }
    
    if (self.passFid.text.length < 1) {
        [WHToast showMessage:@"请输入密码" duration:2 finishHandler:nil];
        return;
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:self.phoneFid.text forKey:@"phoneUserDefaults"];
    
    if (self.loginBlock) {
        self.loginBlock(self.phoneFid.text,self.passFid.text);
    }
}

-(IBAction)forgetPasswordAction:(id)sender{
    
    if(self.forgetPasswordBlock){
        self.forgetPasswordBlock();
    }
}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

    [self.passFid endEditing:YES];
    [self.phoneFid endEditing:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *blank = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
    if(![string isEqualToString:blank]) {
        return NO;
    }
    if (textField.text.length >= 11 && ![string isEqualToString:@""]) {
        return NO;
    }
    return YES;
}
@end
